package cn.iocoder.yudao.module.call.mq.consumer.notify;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.call.controller.admin.billrecord.vo.BillRecordSaveReqVO;
import cn.iocoder.yudao.module.call.mq.message.notify.NotifySendMessage;
import cn.iocoder.yudao.module.call.service.billrecord.BillRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 针对 {@link cn.iocoder.yudao.module.call.mq.message.notify.NotifySendMessage} 的消费者
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class NotifySendConsumer {


    @Resource
    private BillRecordService billRecordService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(NotifySendMessage message) {
        log.info("[NotifySendConsumer][消息内容({})]", message);
        @Valid BillRecordSaveReqVO billRecordSaveReqVO = BeanUtils.toBean(message, BillRecordSaveReqVO.class);
        billRecordService.createBillRecord(billRecordSaveReqVO);
    }

}
